﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using BBMS.DLL.DataContract;
using MySql.Data.MySqlClient;

namespace BBMS.DLL.DataContract
{
    public class ImportDiscountHelper
    {
        public static Hashtable GetDiscountByImport(long id, Base.FilterParams fParams, string strExWhere)
        {
            Hashtable retVal = new Hashtable();
            try
            {
                if (id > 0)
                {
                    MySqlConnection con = new MySqlConnection(BBMS.DLL.Base.Config.ConnectionString());
                    con.Open();

                    MySqlCommand cmd = con.CreateCommand();
                    cmd.CommandText = "select d.CTTHU_ID, d.GiaTriHT, b.MaCT, b.SoCT, b.NgayCT, b.LoaiCT, b.GiaTriCT, b.GiaTriDaTT, b.TrangThaiTT from discount d inner join bill b where d.HDN_ID = @HDN_ID and d.CTTHU_ID = b.ID";
                    cmd.Parameters.Add("@HDN_ID", id);
                    MySqlDataReader rd = cmd.ExecuteReader();
                    while (rd.Read())
                    {
                        Discount obj = new Discount(CommonTools.Helper.StringToLong(rd["CTTHU_ID"].ToString()),
                            //rd["MaCT"].ToString(),
                            //rd["SoCT"].ToString(),
                            (DateTime)rd["NgayCT"],
                            //rd["LoaiCT"].ToString(),
                            CommonTools.Helper.StringToLong(rd["GiaTriCT"].ToString()),
                            CommonTools.Helper.StringToLong(rd["GiaTriDaTT"].ToString()),
                            0,
                            CommonTools.Helper.StringToLong(rd["GiaTriHT"].ToString()), true,
                            (TrangThaiThanhToan)CommonTools.Helper.StringToInt(rd["TrangThaiTT"].ToString()));                        
                        retVal.Add(obj.ID, obj);
                    }
                    rd.Close();
                    rd.Dispose();
                    con.Close();
                    con.Dispose();
                }

                //ChungTuThu ctthu = new ChungTuThu();
                //ctthu.SetExWhereClause(strExWhere);
                //ArrayList arCTThu = ctthu.GetAllObject(fParams);
                /*
                foreach (ChungTuThu obj in arCTThu)
                {
                    if (!retVal.ContainsKey(obj.ID))
                    {
                        Discount dc = new Discount(obj.ID, obj.MaCT, obj.SoCT, obj.NgayCT, obj.LoaiCT, obj.GiaTriCT, obj.GiaTriDaTT,0, 0, false, obj.TrangThaiTT);
                        //( obj.ID,obj.MaCT,obj.SoCT,obj.SoCT,obj.LoaiCT,obj.GiaTriCT,obj.GiaTriDaTT,0,false,obj.TrangThaiTT);
                        retVal.Add(dc.ID, dc);
                    }
                }*/
            }
            catch
            {
            }
            return retVal;
        }
    }

    public class Discount
    {
        public long ID;
        //public string MaCT;
        //public string SoCT;
        public DateTime NgayCT;
        //public string LoaiCT;
        public long GiaTriPhaiThu;
        public long GiaTriDaTT;
        public long GiaTriDaHoachToan;
        public long GiaTriHoachToan;
        public bool Chon = false;
        public TrangThaiThanhToan TrangThaiTT = TrangThaiThanhToan.ChuaTT;
        public Discount()
        {

        }

        public Discount(long id, 
            /*string maCT, string soCT, 
             */
            DateTime ngayCT, 
            /*string loaiCT,*/
            long phaiThu, long daTT, long daHoachToan, long hoachToan, bool chon, TrangThaiThanhToan trangthai)
        {
            this.ID = id;
            //this.MaCT = maCT;
            //this.SoCT = soCT;
            this.NgayCT = ngayCT;
            //this.LoaiCT = loaiCT;
            this.GiaTriPhaiThu = phaiThu;
            this.GiaTriDaTT = daTT;
            this.GiaTriDaHoachToan = daHoachToan;
            this.GiaTriHoachToan = hoachToan;
            this.Chon = chon;
            this.TrangThaiTT = trangthai;
        }
    }

    public class DiscountList
    {
        public long ImportID;
        public bool changed = false ;
        public bool Changed {
            get{
                return changed;
            }
        }
        Hashtable AccountedList = new Hashtable();
        Hashtable AvailableList = new Hashtable();

        public DiscountList(long id)
        {
            ImportID = id;
        }
        public ICollection AccountedValues
        {
            get
            {
                return AccountedList.Values;
            }
        }
        public ICollection AvaiableValues
        {
            get
            {
                return AvailableList.Values;
            }
        }
        public void LoadAccountedDiscount()
        {
            changed = false;

            if (AccountedList != null) AccountedList.Clear();
            else AccountedList = new Hashtable();

            try
            {
                MySql.Data.MySqlClient.MySqlConnection con = 
                    new MySql.Data.MySqlClient.MySqlConnection(BBMS.DLL.Base.Config.ConnectionString());
                con.Open();
                MySqlCommand cmd = con.CreateCommand();
                //cmd.CommandText = "select d.CTTHU_ID, d.GiaTriHT, b.MaCT, b.SoCT,b.NgayCT, b.LoaiCT, b.GiaTriCT, b.GiaTriDaTT, b.TrangThaiTT from discount d inner join bill b where d.HDN_ID = @HDN_ID and d.CTTHU_ID = b.ID";
                cmd.CommandText = "select d.CTTHU_ID, d.GiaTriHT, b.NgayCT, b.GiaTriCT, b.GiaTriDaTT, b.TrangThaiTT from discount d inner join bill b where d.HDN_ID = @HDN_ID and d.CTTHU_ID = b.ID";
                cmd.Parameters.Add("@HDN_ID", this.ImportID);
                MySqlDataReader rd = cmd.ExecuteReader();
                while (rd.Read())
                {
                    Discount obj = new Discount(CommonTools.Helper.StringToLong(rd["CTTHU_ID"].ToString()),
                        //rd["MaCT"].ToString(),
                        //rd["SoCT"].ToString(),
                        (DateTime)rd["NgayCT"],
                        //rd["LoaiCT"].ToString(),
                        CommonTools.Helper.StringToLong(rd["GiaTriCT"].ToString()),
                        CommonTools.Helper.StringToLong(rd["GiaTriDaTT"].ToString()),
                        0,
                        CommonTools.Helper.StringToLong(rd["GiaTriHT"].ToString()), true,
                        (TrangThaiThanhToan)CommonTools.Helper.StringToInt(rd["TrangThaiTT"].ToString()));
                    AccountedList.Add(obj.ID, obj);
                }
                rd.Close();
                rd.Dispose();

                cmd.CommandText = "select sum(GiaTriHT) from discount where CTThu_ID = @CTThu_ID";
                foreach (Discount obj in AccountedList.Values)
                {
                    cmd.Parameters.Clear();
                    cmd.Parameters.Add("@CTThu_ID", obj.ID);
                    try
                    {
                        object daHT = cmd.ExecuteScalar();
                        obj.GiaTriDaHoachToan = CommonTools.Helper.StringToLong(daHT.ToString());
                    }
                    catch { }
                }
                con.Close();
                con.Dispose();
            }
            catch
            {
                AccountedList.Clear();
            }
        }

        public void Insert2DB(MySql.Data.MySqlClient.MySqlCommand cmd)
        {
            cmd.CommandText = "insert into discount (HDN_ID, CTTHU_ID, GiaTriHT) values (@HDN_ID, @CTTHU_ID, @GiaTriHT)";
            foreach (Discount obj in AccountedList.Values)
            {
                cmd.Parameters.Clear();
                cmd.Parameters.Add("@HDN_ID", this.ImportID);
                cmd.Parameters.Add("@CTTHU_ID", obj.ID);
                cmd.Parameters.Add("@GiaTriHT", obj.GiaTriHoachToan);
                cmd.ExecuteNonQuery();
            }
        }

        public void Update2DB(MySql.Data.MySqlClient.MySqlCommand cmd)
        {
            if (changed)
            {
                cmd.CommandText = "delete from discount where HDN_ID = @HDN_ID";
                cmd.Parameters.Clear();
                cmd.Parameters.Add("@HDN_ID", this.ImportID);
                cmd.ExecuteNonQuery();
                if (AccountedList != null && AccountedList.Count > 0)
                {
                    cmd.CommandText = "insert into discount (HDN_ID, CTTHU_ID, GiaTriHT) values (@HDN_ID, @CTTHU_ID, @GiaTriHT)";
                    foreach (Discount obj in AccountedList.Values)
                    {
                        cmd.Parameters.Clear();
                        cmd.Parameters.Add("@HDN_ID", this.ImportID);
                        cmd.Parameters.Add("@CTTHU_ID", obj.ID);
                        cmd.Parameters.Add("@GiaTriHT", obj.GiaTriHoachToan);
                        cmd.ExecuteNonQuery();
                    }
                }
            }
        }
        public void Add(Discount discount)
        {
            if (AccountedList == null) AccountedList = new Hashtable();
            if (AccountedList.ContainsKey(discount.ID))
            {
                Discount obj = AccountedList[discount.ID] as Discount;
                if (obj.GiaTriHoachToan != discount.GiaTriHoachToan)
                {
                    AccountedList.Remove(obj.ID);
                    AccountedList.Add(discount.ID, discount);
                    changed = true;
                }
            }
            else
            {
                AccountedList.Add(discount.ID, discount);
                changed = true;
            }
        }
        public void LoadAvailableDiscount(BBMS.DLL.Base.FilterParams fParams)
        {
            if (AvailableList != null) AvailableList.Clear();
            else AvailableList = new Hashtable();

            try
            {
                MySql.Data.MySqlClient.MySqlConnection con =
                    new MySql.Data.MySqlClient.MySqlConnection(BBMS.DLL.Base.Config.ConnectionString());
                con.Open();
                MySqlCommand cmd = con.CreateCommand();
                cmd.CommandText = "select ID, NgayCT, GiaTriCT, GiaTriDaTT from bill where LoaiCT = 'CTCK' and TrangThaiTT = @TrangThaiTT and " + fParams.SQLString;
                cmd.Parameters.Add("@TrangThaiTT", TrangThaiThanhToan.ChuaTT);
                foreach (Base.FilterCondition fc in fParams.ArParams)
                {
                    cmd.Parameters.Add(fc.Alias, fc.Value);
                }
                MySqlDataReader rd = cmd.ExecuteReader();
                while (rd.Read())
                {
                    Discount obj = new Discount(
                        CommonTools.Helper.StringToLong(rd["ID"].ToString()),
                        //rd["MaCT"].ToString(),
                        //rd["SoCT"].ToString(),
                        (DateTime)rd["NgayCT"],
                        //rd["LoaiCT"].ToString(),
                        CommonTools.Helper.StringToLong(rd["GiaTriCT"].ToString()),
                        CommonTools.Helper.StringToLong(rd["GiaTriDaTT"].ToString()),
                        0,
                        0, 
                        false,
                        TrangThaiThanhToan.ChuaTT);
                    if(AccountedList == null || !AccountedList.ContainsKey(obj.ID))
                        AvailableList.Add(obj.ID, obj);
                }
                rd.Close();
                rd.Dispose();

                cmd.CommandText = "select sum(GiaTriHT) from discount where CTThu_ID = @CTThu_ID";
                foreach (Discount obj in AvailableList.Values)
                {
                    cmd.Parameters.Clear();
                    cmd.Parameters.Add("@CTThu_ID", obj.ID);
                    try
                    {
                        object daHT = cmd.ExecuteScalar();
                        obj.GiaTriDaHoachToan = CommonTools.Helper.StringToLong(daHT.ToString());
                    }
                    catch { }
                }
                con.Close();
                con.Dispose();
            }
            catch
            {
                AvailableList.Clear();
            }
        }
    }
}
